package controller;

import java.io.IOException;
import java.util.List;
import javax.annotation.ManagedBean;
import javax.faces.context.FacesContext;
import model.dao.ProductDao;
import model.domain.Priority;
import model.domain.Product;
import model.domain.Project;


@ManagedBean
public class productBean {

    private Product newproduct;
    private Project project;
    private List<Product> backlogs;
    private Product selectedproduct;

    
    public productBean(){
        this.selectedproduct = new Product();
        this.newproduct = new Product();
    }

    public Product getNewproduct(){
        return this.newproduct;
    }

    public void setNewproduct(Product object){
        this.newproduct = object;
    }

    public void setProject(Project object){
        this.project = object;
    }

    public Project getProject(){
	return this.project;
    }

    public Product getSelectedproduct(){
        return this.selectedproduct;
    }

    public void setSelectedproduct(Product productbacklog){
        this.selectedproduct.setProductID(productbacklog.getProductID());
        this.selectedproduct.setSprint(productbacklog.getSprint());
        this.selectedproduct.setComments(productbacklog.getComments());
        this.selectedproduct.setDescription(productbacklog.getDescription());
        this.selectedproduct.setProject(productbacklog.getProject());
        this.selectedproduct.setPriority(productbacklog.getPriority());
        this.selectedproduct.setSprintbacklogs(productbacklog.getSprintbacklogs());
    }

    public List<Product> getByproject(){

        ProductDao home = new ProductDao();
        this.backlogs = home.findByProject(this.project);

        return this.backlogs;
    }


    public void submitCreate() throws IOException{

        ProductDao home = new ProductDao();

        Product auxProduct = new Product();
        auxProduct.setDescription(this.newproduct.getDescription());
        auxProduct.setPriority(this.newproduct.getPriority());
        auxProduct.setComments(this.newproduct.getComments());
        auxProduct.setProject(this.project);

        home.insert(auxProduct);

        Priority auxPriority = new Priority();
        auxPriority.setPriorityID(1);

        this.newproduct.setDescription("");
        this.newproduct.setPriority(auxPriority);
        this.newproduct.setComments("");
        this.newproduct.setSprint(null);

        FacesContext.getCurrentInstance().getExternalContext().redirect("ProductBacklog.xhtml");
    }

    public void submitEdit(){

        ProductDao home = new ProductDao();
        home.insertOrUpdate(this.selectedproduct);
    }

    public void submitDelete(){
        ProductDao home = new ProductDao();
        home.delete(this.selectedproduct);
    }
}